<?
require_once("include.php");

$me=false;
require_once("header.session.php");
require_once("header.gui.php");
require_once("header.misc.php");
require_once("header.habits.php");

//globals
$MenuObjects= $me->getMyMenu();

if(!$me) {
	$me=&new Person($_SESSION['id']);
}

if(isset($_POST['action'])) {
	switch($_POST['action']) {
		case "habit":
			changeHabitName($_POST['id'],$_POST['name']);
			header("Location: " . $_SERVER['PHP_SELF']);
			die;
			break;
		case "indicator":
			changeIndicatorName($_POST['id'],$_POST['name']);
			$ind=getIndicatorById($_POST['id']);
			header("Location: " . $_SERVER['PHP_SELF'] . "?action=view&type=habit&id=" . $ind['habit_id']);
			die;
			break;
		case "description":
			changeDescription($_POST['id'],$_POST['desc']);
			$desc=getDescById($_POST['id']);
			$ind=getIndicatorById($desc['habit_indicator_id']);
			header("Location: " . $_SERVER['PHP_SELF'] . "?action=view&type=habit&id=" . $ind['habit_id']);
			die;
			break;
	}
}


if(!isset($_GET['action'])) {
	$_GET['action']="defaultmenu";
}

switch($_GET['action']) {
	case "new":
		switch($_GET['type']) {
			case "habit":
				header("Location: " . $_SERVER['PHP_SELF'] . "?action=edit&type=habit&id=" . newHabit());
				die;
				break;
			case "indicator":
				header("Location: " . $_SERVER['PHP_SELF'] . "?action=edit&type=indicator&id=" . newIndicator($_GET['habit']));
				die;
				break;
			case "description":
				header("Location: " . $_SERVER['PHP_SELF'] . "?action=edit&type=description&id=" . newDescription($_GET['ind'],$_GET['lev']));
				die;
				break;
		}
		break;
	
	case "delete":
		switch($_GET['type']) {
			case "indicator":
				deleteIndicator($_GET['id']);
				header("Location: " . $_SERVER['PHP_SELF'] . "?action=view&type=habit&id=" . $_GET['habitid']);
				die;
				break;
			case "habit":
				deleteHabit($_GET['id']);
				header("Location: " . $_SERVER['PHP_SELF']);
				die;
				break;
		}
		break;
}
		

function Headers() {
	
}

function Body() {
	switch($_GET['action']) {
		case "view":
			switch($_GET['type']) {
				case "main":
					require_once("habits_Main.php");
					break;
				case "habit":
					require_once("habits_Habit.php");
					break;
				default:
					require_once("habits_Main.php");
					break;
			}
			break;
		case "edit":
			switch($_GET['type']) {
				case "habit":
					?>
<form action="<? echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="action" value="habit">
<input type="hidden" name="id" value="<? echo $_GET['id'] ?>">
<input type="text" name="name" value="<?
$habit=getHabitById($_GET['id']);
echo $habit['habit_name'];
?>">
<input type="submit" value="Change Name">
</form>
<?
					break;
				case "indicator":
					?>
<form action="<? echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="action" value="indicator">
<input type="hidden" name="id" value="<? echo $_GET['id'] ?>">
<input type="text" name="name" style="width:350" value="<?
$habit=getIndicatorById($_GET['id']);
echo $habit['habit_indicator_name'];
?>"><br />
<input type="submit" value="Change Name">
</form>
<?
					break;
				case "description":
					?>
<form action="<? echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="action" value="description">
<input type="hidden" name="id" value="<? echo $_GET['id'] ?>">
<textarea name="desc"><?
$habit=getDescById($_GET['id']);
echo $habit['habit_description_description'];
?>
</textarea><br>
<input type="submit" value="Change Description">
</form>
<?
					break;
				default:
					require_once("habits_Main.php");
					break;
			}
			break;
		default:
			require_once("habits_Main.php");
			break;
	}
}

include("template.php");